<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="btn" uri="http://www.dsdl.com/dev/jsp/btn" %>
<%@include file="../base/base.jspf" %>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">单位编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="unitNo" placeholder="请输入单位编码" autocomplete="off" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">单位名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="unitName" placeholder="请输入单位名称" autocomplete="off"
                               class="layui-input"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="unit-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <script type="text/html" id="toolbar-unitListPage-unit">
                <div class="layui-btn-container">
                    <btn:btn btnStr="unitAdd">
                        <button class="layui-btn" lay-event="add">新增</button>
                    </btn:btn>
                </div>
            </script>
            <table id="unit-page-data" lay-filter="unit-page-data"></table>
            <script type="text/html" id="table-unit-webuser">
                <btn:btn btnStr="unitEdit">
                    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit"><i
                            class="layui-icon layui-icon-edit"></i>编辑</a>
                </btn:btn>
                <btn:btn btnStr="unitDelete">
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                            class="layui-icon layui-icon-delete"></i>删除</a>
                </btn:btn>
            </script>
        </div>
    </div>
</div>
<script type="text/javascript">
    layui.config({
        base: "${ctx}/static/framework/layuiadmin/" //静态资源所在路径
    }).extend({
        index: "lib/index" //主入口模块
    }).use(["index", "table", "form"], function () {
        var table = layui.table, form = layui.form;
        table.render({
            elem: "#unit-page-data",
            url: "${ctx}/unit/getUnitList",
            method: "post",
            toolbar: "#toolbar-unitListPage-unit",
            defaultToolbar: ["print"],
            height: "full-125",//自己根据表格上方和下方的高度减去对应数值，保证侧边无滚动条即可
            cols: [[{
                field: "unitNo",
                title: "单位编号",
                width: 150,
                align: "center",
                sort: true
            }, {
                field: "unitName",
                align: "center",
                title: "单位名称"
            }, {
                title: "操作",
                width: 200,
                align: "center",
                fixed: "right",
                toolbar: "#table-unit-webuser"
            }]],
            page: true,
            limit: 20,
            limits: [20, 50, 100],
            text: {
                none: "暂无相关数据"
            }
        });

        form.on("submit(unit-front-search)", function (data) {
            table.reload("unit-page-data", {
                where: data.field,
                page: {curr: 1}
            });
        });

        table.on("tool(unit-page-data)", function (obj) {
            var data = obj.data;
            if (obj.event == "del") {
                layer.confirm("您确定要删除该单位吗？", {
                    icon: 3,
                    title: "提示",
                    btn: ["确定", "取消"] //按钮
                }, function () {
                    tip_index = load();
                    $.ajax({
                        async: true,
                        url: "${ctx}/unit/unitDelete",
                        type: "post",
                        dataType: "json",
                        data: {
                            id: data.id
                        },
                        success: function (data) {
                            layer.close(tip_index);
                            if (data.result) {
                                layer.msg(data.info);
                                table.reload("unit-page-data");
                            } else {
                                layer.msg(data.info);
                            }
                        }
                    });
                }, function (index) {
                    layer.close(index);
                });
            } else if (obj.event == "edit") {
                var data = obj.data;
                layer.open({
                    type: 2,
                    title: "单位修改",
                    offset: "t",
                    content: "${ctx}/unit/unitSubmitPage?id=" + data.id,
                    area: ["500px", "300px"],
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        layero.find("iframe").contents().find("#unit-front-submit").trigger("click");
                    }
                });
            }
        });

        table.on("toolbar(unit-page-data)", function (obj) {
            if (obj.event == "add") {
                layer.open({
                    type: 2,
                    title: "添加单位",
                    offset: "t",
                    content: "${ctx}/unit/unitSubmitPage",
                    area: ["500px", "300px"],
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        layero.find("iframe").contents().find("#unit-front-submit").trigger("click");
                    }
                });
            }
        });
    });
</script>